{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<center><img src=\"../logo.png\" alt=\"Header\" style=\"width: 800px;\"/></center>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "@Copyright (C): 2010-2020, Shenzhen Yahboom Tech  \n",
    "@Author: Liusen  \n",
    "@Date: 2020-02-04 16:20:02  \n",
    "@LastEditors: Liusen  \n",
    "@LastEditTime: 2020-02-04 16:20:02   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import RPi.GPIO as GPIO\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "GPIO.setwarnings(False)\n",
    "GPIO.setmode(GPIO.BCM)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "class CarUltrasound(object):\n",
    "    def __init__(self):\n",
    "\n",
    "        # ³¬Éù²¨Òý½Å¶¨Òå\n",
    "        self.GPIO_TRIGGER = 1  # GPIO setting (BCM coding)\n",
    "        self.GPIO_ECHO = 0\n",
    "\n",
    "        GPIO.setup(self.GPIO_TRIGGER, GPIO.OUT)  # GPIO input/output definiation\n",
    "        GPIO.setup(self.GPIO_ECHO, GPIO.IN)\n",
    "\n",
    "        self.dist_mov_ave = 0\n",
    "\n",
    "    def distMeasure(self):  # distance measuing\n",
    "\n",
    "        GPIO.output(self.GPIO_TRIGGER, GPIO.HIGH)\n",
    "        time.sleep(0.000015)\n",
    "        GPIO.output(self.GPIO_TRIGGER, GPIO.LOW)\n",
    "        while not GPIO.input(self.GPIO_ECHO):\n",
    "            pass\n",
    "        t1 = time.time()\n",
    "        while GPIO.input(self.GPIO_ECHO):\n",
    "            pass\n",
    "        t2 = time.time()\n",
    "        print\n",
    "        \"distance is %d \" % (((t2 - t1) * 340 / 2) * 100)\n",
    "        time.sleep(0.01)\n",
    "        return ((t2 - t1) * 340 / 2) * 100\n",
    "\n",
    "        # GPIO.output(self.GPIO_TRIGGER, False)\n",
    "        # time.sleep(0.000002)\n",
    "        # GPIO.output(self.GPIO_TRIGGER, True)  # emit ultrasonic pulse\n",
    "        # time.sleep(0.00001)                   # last 10us\n",
    "        # GPIO.output(self.GPIO_TRIGGER, False) # end the pulse\n",
    "\n",
    "        # ii = 0\n",
    "        # while GPIO.input(self.GPIO_ECHO) == 0:  # when receiving the echo, ECHO will become 1\n",
    "        #     ii = ii + 1\n",
    "        #     if ii > 10000:\n",
    "        #         print('Ultrasound error: the sensor missed the echo')\n",
    "        #         return 0\n",
    "        #     pass\n",
    "        # start_time = time.time()\n",
    "        #\n",
    "        # while GPIO.input(self.GPIO_ECHO) == 1:  # the duration of high level of ECHO is the time between the emitting the pulse and receiving the echo\n",
    "        #         pass\n",
    "        # stop_time = time.time()\n",
    "        #\n",
    "        # time_elapsed = stop_time - start_time\n",
    "        # distance = (time_elapsed * 34300) / 2\n",
    "        #\n",
    "        # return distance\n",
    "\n",
    "    def distMeasureMovingAverage(self):\n",
    "        dist_current = self.distMeasure()\n",
    "        if dist_current == 0:  # if the sensor missed the echo, the output dis_mov_ave will equal the last dis_mov_ave\n",
    "            return self.dist_mov_ave\n",
    "        else:\n",
    "            self.dist_mov_ave = 0.8 * dist_current + 0.2 * self.dist_mov_ave  # using the moving average of distance measured by sensor to reduce the error\n",
    "            return self.dist_mov_ave"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Measured Distance = 220.68 cm\n",
      "Measured Distance = 265.26 cm\n",
      "Measured Distance = 257.99 cm\n",
      "Measured Distance = 258.46 cm\n",
      "Measured Distance = 265.66 cm\n",
      "Measured Distance = 59.21 cm\n",
      "Measured Distance = 57.48 cm\n",
      "Measured Distance = 560.40 cm\n",
      "Measured Distance = 5.05 cm\n",
      "Measured Distance = 9.02 cm\n",
      "Measured Distance = 9.51 cm\n",
      "Measured Distance = 8.37 cm\n",
      "Measured Distance = 560.97 cm\n",
      "Measured Distance = 9.35 cm\n",
      "Measured Distance = 45.26 cm\n",
      "Measured Distance = 14.97 cm\n",
      "Measured Distance = 17.01 cm\n",
      "Measured Distance = 17.65 cm\n",
      "Measured Distance = 17.75 cm\n",
      "Measured Distance = 19.45 cm\n",
      "Measured Distance = 18.24 cm\n",
      "Measured Distance = 42.14 cm\n",
      "Measured Distance = 42.49 cm\n",
      "Measured Distance = 52.26 cm\n",
      "Measured Distance = 37.96 cm\n",
      "Measured Distance = 42.20 cm\n",
      "Measured Distance = 42.59 cm\n",
      "Measured Distance = 228.51 cm\n",
      "Measured Distance = 60.03 cm\n",
      "Measured Distance = 57.71 cm\n",
      "Measured Distance = 51.91 cm\n",
      "Measured Distance = 560.99 cm\n",
      "Measured Distance = 74.89 cm\n",
      "Measured Distance = 57.23 cm\n",
      "Measured Distance = 67.61 cm\n",
      "Measured Distance = 56.84 cm\n",
      "Measured Distance = 62.37 cm\n",
      "Measured Distance = 98.74 cm\n",
      "Measured Distance = 561.00 cm\n",
      "Measured Distance = 59.99 cm\n",
      "Measured Distance = 51.52 cm\n",
      "Measured Distance = 228.59 cm\n",
      "Measured Distance = 87.88 cm\n",
      "Measured Distance = 84.34 cm\n",
      "Measured Distance = 84.43 cm\n",
      "Measured Distance = 59.28 cm\n",
      "Measured Distance = 52.38 cm\n",
      "Measured Distance = 13.43 cm\n",
      "Measured Distance = 4.64 cm\n",
      "Measured Distance = 4.51 cm\n",
      "Measured Distance = 5.46 cm\n",
      "Measured Distance = 11.20 cm\n",
      "Measured Distance = 7.23 cm\n",
      "Measured Distance = 9.80 cm\n",
      "Measurement stopped by User\n"
     ]
    }
   ],
   "source": [
    "if __name__ == '__main__':\n",
    "    try:\n",
    "        car = CarUltrasound()\n",
    "        while True:\n",
    "            dist = car.distMeasure()\n",
    "            print(\"Measured Distance = {:.2f} cm\".format(dist))\n",
    "            time.sleep(1)\n",
    "\n",
    "        # Reset by pressing CTRL + C\n",
    "    except KeyboardInterrupt:\n",
    "        print(\"Measurement stopped by User\")\n",
    "        GPIO.cleanup()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
